<script>
	import {
		API_URL,
		IMG_URL
	} from './env.js'
	export default {
		globalData: {
			height: 0,
			// head:'https://api.jinyiweikeji.com',
			// head2:'https://api.jinyiweikeji.com/ywt',
			head: API_URL,
			head2: API_URL + '/ywt',
			isIpx: false
		},
		onLaunch: function(e) {
			console.log('API_URL', this.globalData.head2)

			if (uni.getStorageSync('token')) {
				this.getunit()
				this.getdic()
				this.getuser()

				this.getCompany()
				setTimeout(() => {
					this.getTeam()

				}, 200)
			}



			wx.setInnerAudioOption({
				obeyMuteSwitch: false,
				success: (res) => {
					console.log(res)
				},
				fail: (err) => {
					console.log(err)
				}
			})
			console.log('App Launch')
			console.log(uni.getStorageSync('dic'))
			//#ifdef MP-WEIXIN || MP-QQ

			// 获取系统信息，底部提交按钮做适配
			wx.getSystemInfo({
				success: (res) => {
					// console.log(res)
					let modelmes = res.model; //手机品牌

					if (modelmes.indexOf('iPhone X') != -
						1) { //XS,XR,XS MAX均可以适配,因为indexOf()会将包含'iPhone X'的字段都查出来
						this.globalData.isIpx = true
						console.log('手机品牌', modelmes, this.globalData.isIpx)
					}
				},
			})
			//#endif
			//#ifdef MP-WEIXIN
			this.updateManager()
			//#endif


		},
		onShow: function(e) {
			console.log(e)

		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			getunit() {
				//unit 为科室部门信息，dic为字典信息，user为人员列表信息
				var header = {}
				var data = {}
				if (uni.getStorageSync('token')) {
					header = {
						'content-type': 'application/x-www-form-urlencoded',
						token: uni.getStorageSync('token')
					}
					data = {
						dicList: 'unit'
					}
				} else {
					header = {
						'content-type': 'application/x-www-form-urlencoded'
					}
					data = {
						dicList: 'unit',
						hospitalId: uni.getStorageSync('hospitalId') ? uni.getStorageSync('hospitalId') : ''
					}
				}
				uni.request({
					url: this.globalData.head2 + '/outer/getEditionList',
					header,
					method: 'POST',
					data,
					success: (res) => {
						if (res.data.code == 0) {
							var list = res.data.body.unit
							uni.setStorageSync('unit', list)
						}
					}
				})


			},
			getdic() {
				var header = {}
				var data = {}
				if (uni.getStorageSync('token')) {
					header = {
						'content-type': 'application/x-www-form-urlencoded',
						token: uni.getStorageSync('token')
					}
					data = {
						dicList: 'dic'
					}
				} else {
					header = {
						'content-type': 'application/x-www-form-urlencoded'
					}
					data = {
						dicList: 'dic',
						hospitalId: uni.getStorageSync('hospitalId') ? uni.getStorageSync('hospitalId') : ''
					}
				}
				uni.request({
					url: this.globalData.head2 + '/outer/getEditionList',
					header,
					method: 'POST',
					data,
					success: (res) => {
						if (res.data.code == 0) {
							var list = res.data.body.dic
							uni.setStorageSync('dic', list)

						}
					}
				})
			},
			getuser() {
				var header = {}
				var data = {}
				if (uni.getStorageSync('token')) {
					header = {
						'content-type': 'application/x-www-form-urlencoded',
						token: uni.getStorageSync('token')
					}
					data = {
						dicList: 'user'
					}
				} else {
					header = {
						'content-type': 'application/x-www-form-urlencoded'
					}
					data = {
						dicList: 'user',
						hospitalId: uni.getStorageSync('hospitalId') ? uni.getStorageSync('hospitalId') : ''
					}
				}
				uni.request({
					url: this.globalData.head2 + '/outer/getEditionList',
					header,
					method: 'POST',
					data,
					success: (res) => {
						if (res.data.code == 0) {
							var list = res.data.body.user
							uni.setStorageSync('user', list)
						}
					}
				})
			},
			getCompany() {
				uni.request({
					url: this.globalData.head2 + '/sys/dic/manufacturer/list/output',
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						token: uni.getStorageSync('token')
					},
					method: 'POST',
					data: {
						dicList: 'user'
					},
					success: (res) => {
						if (res.data.code == 0) {
							var list = res.data.body
							uni.setStorageSync('company', list)
						}
					}
				})
			},
			getTeam() {

				uni.request({
					url: this.globalData.head2 + '/sysManager/list/teamUser',
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						token: uni.getStorageSync('token')
					},
					method: 'POST',
					success: (res) => {
						// console.log('获取小组信息',JSON.stringify(res))
						if (res.data.code == 0) {
							var list = res.data.body
							var team = []
							list.forEach(item => {
								var flag = true
								team.forEach(item2 => {
									if (item2.teamId == item.teamId) {
										flag = false
									}
								})
								if (flag) {
									team.push({
										teamId: item.teamId,
										teamName: item.teamName,
										children: []
									})
								}
							})
							team = team.sort((a, b) => {
								return a.teamId - b.teamId
							})
							team.forEach(item => {
								item.name = item.teamName //处理名字，用于picker
								list.forEach(item2 => {
									item2.name = item2.userName //处理名字，用于picker
									if (item2.teamId == item.teamId) {
										item.children.push(item2)
									}
								})
							})
							team.forEach(item => {
								if (!item.teamId) {
									item.teamId = 0
									item.teamName = "未分组工程师" //处理名字，用于picker
									item.name = "未分组工程师"
								} else {
									item.children.unshift({

										name: '选择该小组',
										userName: item.teamName,
										teamId: item.teamId

									})
								}
							})
							uni.setStorageSync('team', team)
						}
					}
				})
			},
			/**
			 * 小程序主动更新
			 */
			updateManager() {
				console.log('小程序主动更新')
				if (!wx.canIUse('getUpdateManager')) {
					return false;
				}
				const updateManager = wx.getUpdateManager();
				updateManager.onCheckForUpdate(function(res) {
					// 请求完新版本信息的回调
					// console.log(res.hasUpdate)
				});
				updateManager.onUpdateReady(function() {
					wx.showModal({
						title: '更新提示',
						content: '新版本已经准备好，即将重启应用',
						showCancel: false,
						success(res) {
							if (res.confirm) {
								// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
								updateManager.applyUpdate()
							}
						}
					});
				});
				updateManager.onUpdateFailed(function() {
					// 新的版本下载失败
					wx.showModal({
						title: '更新提示',
						content: '新版本下载失败',
						showCancel: false
					})
				});
			},
		}
	}
</script>

<style lang="scss">
	@import "uview-ui/index.scss";

	page {
		background-color: #f5f5f5;
	}

	/*每个页面公共css */
	view,
	text,
	image,
	ul,
	li,
	input,
	textarea,
	scroll-view {
		box-sizing: border-box;
	}

	.m-top5 {
		margin-top: 5rpx;
	}

	.m-top10 {
		margin-top: 10rpx;
	}

	.m-top20 {
		margin-top: 20rpx;
	}

	.m-top30 {
		margin-top: 30rpx;
	}

	.m-left5 {
		margin-left: 5rpx;
	}

	.m-left10 {
		margin-left: 10rpx;
	}

	.m-left20 {
		margin-left: 20rpx;
	}

	.m-left30 {
		margin-left: 30rpx;
	}

	.m-right5 {
		margin-right: 5rpx;
	}

	.m-right10 {
		margin-right: 10rpx;
	}

	.m-right20 {
		margin-right: 20rpx;
	}

	.m-right30 {
		margin-right: 30rpx;
	}

	.m-bottom5 {
		margin-bottom: 5rpx;
	}

	.m-bottom10 {
		margin-bottom: 10rpx;
	}

	.m-bottom20 {
		margin-bottom: 20rpx;
	}

	.m-bottom30 {
		margin-bottom: 30rpx;
	}

	.p-top5 {
		padding-top: 5rpx;
	}

	.p-top10 {
		padding-top: 10rpx;
	}

	.p-top20 {
		padding-top: 20rpx;
	}

	.p-bottom5 {
		padding-bottom: 5rpx;
	}

	.p-bottom10 {
		padding-bottom: 10rpx;
	}

	.p-bottom20 {
		padding-bottom: 20rpx;
	}

	.p-bottom30 {
		padding-bottom: 30rpx;
	}

	.p-left10 {
		padding-left: 10rpx;
	}

	.p-left20 {
		padding-left: 20rpx;
	}

	.p-right10 {
		padding-right: 10rpx;
	}

	.p-right20 {
		padding-right: 20rpx;
	}

	.line-through {
		text-decoration: line-through;
	}

	.one {
		text-overflow: ellipsis;
		overflow: hidden;
		white-space: nowrap;
	}

	.two {
		text-overflow: -o-ellipsis-lastline;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		line-clamp: 2;
		-webkit-box-orient: vertical;
		overflow: hidden;
	}

	.flex {
		display: flex;

	}

	.align-flex {
		display: flex;
		align-items: center;
	}

	.center-flex {
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.between-flex {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.colum-flex {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.f24 {
		font-size: 24rpx;
	}

	.f32 {
		font-size: 32rpx;
	}

	.f36 {
		font-size: 36rpx;
	}

	.f34 {
		font-size: 34rpx;
	}

	.f28 {
		font-size: 28rpx;
	}

	.f26 {
		font-size: 26rpx;
	}

	.f22 {
		font-size: 22rpx;
	}

	.f20 {
		font-size: 20rpx;
	}

	.f18 {
		font-size: 18rpx;
	}

	.bold {
		font-weight: bold;
	}

	.c999 {
		color: #999999;
	}

	.cfff {
		color: #ffffff;
	}

	.ccc {
		color: #c0c4cc;
	}

	.c666 {
		color: #737373;
	}

	.c333 {
		color: #333 !important;
	}

	.c000 {
		color: #000 !important;
	}

	.blue {
		color: #409EFF;
	}

	.red {
		color: #F56C6C;

	}

	.gray {
		color: #98A5B3;
	}

	.textColor {
		color: #0F2E4D;
	}

	.bgblue {
		background-color: #409EFF;
	}

	.bgwhite {
		background-color: white;
	}

	radio {
		transform: scale(0.8);
	}

	.lbtn {
		width: 690rpx;
		height: 88rpx;
		margin: 40rpx auto;
		background: #409eff;
		border-radius: 44rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #fff;
		font-size: 34rpx;
		font-weight: 600;
	}

	textarea {}

	/*checkbox 整体大小  */
	// checkbox {
	// 	width: 50rpx;
	// 	height: 50rpx;
	// 	border-radius: 50%;
	// }

	/*checkbox 选项框大小  */
	// checkbox .wx-checkbox-input {
	// 	width: 50rpx;
	// 	height: 50rpx;
	// 	border-radius: 50%;
	// }

	/*checkbox选中后样式  */
	// checkbox .wx-checkbox-input.wx-checkbox-input-checked {
	// 	background: #409eff;
	// 	border-radius: 50%;
	// }

	/*checkbox选中后图标样式  */
	// checkbox .wx-checkbox-input.wx-checkbox-input-checked::before {
	// 	width: 28rpx;
	// 	height: 28rpx;
	// 	line-height: 28rpx;
	// 	text-align: center;
	// 	font-size: 22rpx;
	// 	border-radius: 50%;
	// 	color: #fff;
	// 	background: transparent;
	// 	transform: translate(-50%, -50%) scale(1);
	// 	-webkit-transform: translate(-50%, -50%) scale(1);
	// }

	.choosearea {
		width: 500rpx;
		text-align: right;
		justify-content: flex-end;
	}

	/* ios安全距离 */
	.footerbox {
		position: fixed;
		width: 100%;
		left: 0;
		bottom: 0;
		background-color: #f5f5f5;
	}

	.space {
		height: calc(constant(safe-area-inset-bottom) + 200rpx);

		height: calc(env(safe-area-inset-bottom) + 200rpx);
	}

	.paddingBottom {

		padding-bottom: constant(safe-area-inset-bottom) !important;

		padding-bottom: env(safe-area-inset-bottom) !important;



	}

	.marginBottom {

		margin-bottom: constant(safe-area-inset-bottom);

		margin-bottom: env(safe-area-inset-bottom);



	}

	.fixedBottom {

		bottom: calc(constant(safe-area-inset-bottom) + 20px);

		bottom: calc(env(safe-area-inset-bottom) + 20px);



	}
</style>
